package com.feishi.core.media.javacv.util;

import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_imgcodecs.cvLoadImage;
import static org.bytedeco.javacpp.opencv_imgproc.CV_TM_CCORR_NORMED;
import static org.bytedeco.javacpp.opencv_imgproc.cvMatchTemplate;

/**
 * @version 1.0
 * @auth caixq
 * @time 18-5-31 16:40
 **/


public class TemplateMatch {


    public static boolean matchTemplate(IplImage image,IplImage source) {
        boolean matchRes;
        IplImage result = cvCreateImage(cvSize(
                source.width() - image.width() + 1,
                source.height() - image.height() + 1),
                IPL_DEPTH_32F, 1);

        cvZero(result);
        cvMatchTemplate(source, image, result, CV_TM_CCORR_NORMED);
        double[] minVal = new double[2];
        double[] maxVal = new double[2];

        cvMinMaxLoc(result, minVal, maxVal);
        matchRes = maxVal[0] > 0.99f ? true : false;
        cvReleaseImage(result);
        return matchRes;
    }
}